//
//  BQSearchController.m
//  BeeQuick
//
//  Created by mac on 16/11/24.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQSearchController.h"
#import "BQSearchResultController.h"
#import "BQMarketViewController.h"
#import <PYSearch.h>
@interface BQSearchController ()<PYSearchViewControllerDelegate>

@property(nonatomic,strong)NSArray * array;

@end

@implementation BQSearchController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *hotSeaches   = @[@"aaa",@"bbb"];
    // 2. Create searchViewController
    PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"请输入商品关键字" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // Call this Block when completion search automatically
        // Such as: Push to a view controller
        [searchViewController.navigationController pushViewController:[[BQSearchResultController alloc] init] animated:YES];
        
    }];
    // 3. present the searchViewController
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    [self presentViewController:nav  animated:NO completion:nil];
    searchViewController.delegate = self;
    searchViewController.hotSearchStyle = PYHotSearchStyleColorfulTag;
}


- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    if (searchText.length) { // 与搜索条件再搜索
        // 根据条件发送查询（这里模拟搜索）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 搜素完毕
            // 显示建议搜索结果
            NSMutableArray *searchSuggestionsM = [NSMutableArray array];
            for (int i = 0; i < arc4random_uniform(5) + 10; i++) {
                NSString *searchSuggestion = [NSString stringWithFormat:@"搜索建议 %d", i];
                [searchSuggestionsM addObject:searchSuggestion];
            }
            // 返回
            searchViewController.searchSuggestions = searchSuggestionsM;
        });
    }
}
@end
